package com.example.demozmq.zmq;

import java.nio.charset.StandardCharsets;
import java.util.Random;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

public class ZmqPublisher {

    public static void main(String[] args) throws InterruptedException {
        Context context = ZMQ.context(1);
        Socket socket = context.socket(ZMQ.PUB);
        // 绑定端口
        socket.bind("tcp://*:5556");
        Random random = new Random(1000);
        while (true) {
            // 随机生成一个整数
            int value = random.nextInt();
            // 将整数作为消息发布到通道上
            byte[] topic = "value".getBytes(StandardCharsets.UTF_8);
            byte[] data = Integer.toString(value).getBytes(StandardCharsets.UTF_8);
            socket.sendMore(topic);
            socket.send(data);
            System.out.println("发送整数：| " + value);
            Thread.sleep(3000);
        }
    }

}
